﻿using System.Linq;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Outlook;
using Outlook.WorkItems.Extension.Addin.Actions;

namespace Outlook.WorkItems.Extension.Addin
{
    partial class BowieRibbonExtensibility
    {
        public void trackInWorkItemAppointmentItem_Action(IRibbonControl control, bool isPressed)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return;

            if (isPressed)
            {
                var action = new TrackInWorkItemAction(new OutlookAppointmentItem(item));
                action.Do();
            }
            else
            {
                var action = new UntrackWorkItemAction(new OutlookAppointmentItem(item));
                action.Do();
            }
        }

        public bool trackInWorkItemAppointmentItemButton_GetPressed(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return false;

            return item.ItemProperties["RelatedWIIds"] != null && item.ItemProperties["RelatedWIIds"].Value != "" && item.ItemProperties["RelatedWIIds"].Value != null;
        }

        public void relatedWorkItemAppointmentItemButton_Action(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return;

            var action = new ViewRelatedWorkItemAction(item.ItemProperties);
            action.Do();
        }

        public bool relatedWorkItemAppointmentItemButton_GetEnabled(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return false;

            return item.ItemProperties["RelatedWIIds"] != null && item.ItemProperties["RelatedWIIds"].Value != "" && item.ItemProperties["RelatedWIIds"].Value != null;
        }

        public void insertReportAppointmentItemButton_Action(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return;

            var action = new InsertReportAction(item.Attachments);
            action.Do();
        }

        public void downloadAttachmentAppointmentItemButton_Action(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return;

            var action = new DownloadAttachmentsFromTeamPortalAction(item.Attachments);
            action.Do();

            if (string.IsNullOrEmpty(action.HtmlLink))
                return;

            if (action.HtmlLinks != null && action.HtmlLinks.Count > 0)
            {
                foreach (var htmlLink in action.HtmlLinks)
                {
                    item.Body += string.Format("\r\n{0} : {1}", htmlLink.Item1, htmlLink.Item2);
                }
            }
        }

        public void sendAttachmentAppointmentItemButton_Action(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return;

            var attachments = item.Attachments.Cast<Attachment>();
            var action = new UploadAttachmentsToTeamPortalAction(attachments);
            action.Do();
        }

        public bool sendAttachmentAppointmentItemButton_GetEnabled(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return false;

            return item.Attachments != null && item.Attachments.Count > 0;
        }

        public void showBuildReportAppointmentItemButton_Action(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return;

            var prop = item.UserProperties.Find("BuildUri");
            if (prop == null || prop.Value == null) 
                return;

            var action = new ShowBuildReportAction(prop);
            action.Do();
        }

        public bool showBuildReportAppointmentItemButton_GetEnabled(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return false;

            var prop = item.UserProperties.Find("BuildUri");
            return prop != null && prop.Value != null;
        }

        public bool qualityBuildAppointmentItemDropDown_GetEnabled(IRibbonControl control)
        {
            AppointmentItem item;
            if (!TryGetAppointmentFromInspector(control.Context, out item))
                return false;

            var prop = item.UserProperties.Find("BuildUri");
            return prop != null && prop.Value != null;
        }

        public static bool TryGetAppointmentFromInspector(dynamic context, out AppointmentItem item)
        {
            item = null;

            var inspector = context as Inspector;
            if (inspector == null)
                return false;
            
            item = inspector.CurrentItem as AppointmentItem;
            return item != null;
        }
    }
}
